/* This file is part of GSMC.
 * 
 * GSMC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GSMC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GSMC.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.hpxn.gsmc.config;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import net.hpxn.gsmc.config.map.Map;
import net.hpxn.gsmc.config.notifier.NotifierConfig;
import net.hpxn.gsmc.config.server.ServerConfigs;
import net.hpxn.gsmc.config.usercountlogger.UserCountLoggerConfig;

/**
 * Returns the configurations in the XML files. If changes are made in the XML
 * files a restart is required.
 * 
 * @author peterdi
 * 
 */
public class ConfigFactory {

	private static Map mapOrm;
	private static ServerConfigs serversOrm;
	private static NotifierConfig notifierOrm;
	private static UserCountLoggerConfig userCountLoggerOrm;
	public static final String VERSION = "04-21-2011";


	public ConfigFactory() { }
	
	public static UserCountLoggerConfig getUserCountLoggerConfig() throws Exception  {
		if ( userCountLoggerOrm == null ) {
			JAXBContext jc = JAXBContext
					.newInstance( "net.hpxn.gsmc.config.usercountlogger" );
			Unmarshaller u = jc.createUnmarshaller();
			File wXmlFile = new File( "configs" + File.separatorChar
					+ "usercountlogger.xml" );
			userCountLoggerOrm = (UserCountLoggerConfig) u.unmarshal( wXmlFile );
		}
		return userCountLoggerOrm;
	}
	
	public static NotifierConfig getNotifierConfig() throws Exception  {
		if ( notifierOrm == null ) {
			JAXBContext jc = JAXBContext
					.newInstance( "net.hpxn.gsmc.config.notifier" );
			Unmarshaller u = jc.createUnmarshaller();
			File wXmlFile = new File( "configs" + File.separatorChar
					+ "notifier.xml" );
			notifierOrm = (NotifierConfig) u.unmarshal( wXmlFile );
		}
		return notifierOrm;
	}

	public static Map getMapConfig( String pType ) throws Exception {
		if ( mapOrm == null ) {
			JAXBContext jc = JAXBContext
					.newInstance( "net.hpxn.gsmc.config.map" );
			Unmarshaller u = jc.createUnmarshaller();
			File wMapFile = new File( "maps" + File.separatorChar + pType
					+ "_map.xml" );
			mapOrm = (Map) u.unmarshal( wMapFile );
		}
		return mapOrm;
	}

	public static ServerConfigs getServerConfigs() throws Exception  {
		if ( serversOrm == null ) {
			JAXBContext jc = JAXBContext
					.newInstance( "net.hpxn.gsmc.config.server" );
			Unmarshaller u = jc.createUnmarshaller();
			File wXmlFile = new File( "configs" + File.separatorChar
					+ "server.xml" );
			serversOrm = (ServerConfigs) u.unmarshal( wXmlFile );
		}
		return serversOrm;
	}
}
